//
//  FCCreditScoreViewController.swift
//  FinancialConsulting
//
//  Created by Max on 2017/10/31.
//  Copyright © 2017年 com.vector. All rights reserved.
//

import UIKit

class FCCreditScoreViewController: FCViewController ,UITableViewDataSource, UITableViewDelegate , FCCreditScoreCellTwoDelegate {

    @IBOutlet weak var tablebView: FCTableView!
    
    let array1: NSArray = ["3000以下",
                           "3000~5000",
                           "5000~8000",
                           "8000及以上"]
    
    let array2: NSArray = ["有",
                           "无"]
    let array3: NSArray = ["有",
                           "无"]
    
    let array4: NSArray = ["有",
                           "无"]
    
    let array5: NSArray = ["有",
                           "无"]
    
    
    var textFiledArr: NSMutableArray!
   
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "信用分值"
        tablebView.tableFooterView = UIView()
        textFiledArr = NSMutableArray.init()

    }

    func numberOfSections(in tableView: UITableView) -> Int {
        
        return 2
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if section == 0 {
            
            return 10
            
        }else {
            
            
            return 2
            
        }
    }
    

   
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = UITableViewCell()
        
        switch  indexPath.section {
        case 0:
            cell = tableView.dequeueReusableCell(withIdentifier: FCCreditScoreCell.name,for: indexPath)
            if let creditScoreCell = cell as? FCCreditScoreCell {
                
                creditScoreCell.titleText.tag = indexPath.row
                textFiledArr.add(creditScoreCell.titleText)
                
                if indexPath.row == 0 {
                    
                    creditScoreCell.titleLabel.text = "姓名"
                    
                    if  FCModelData.instance.name.characters.count > 0 {
                    
                        creditScoreCell.titleText.text = FCModelData.instance.name
                        
                    }else {
                        creditScoreCell.titleText.placeholder = "请输入姓名"
                        
                    }
                    
                    
                }else if indexPath.row == 1 {
                    creditScoreCell.titleLabel.text = "身份证号"
                    
                    
                    if  FCModelData.instance.idNumber.characters.count > 0 {
                        
                        creditScoreCell.titleText.text = FCModelData.instance.idNumber
                        
                    }else {
                        creditScoreCell.titleText.placeholder = "请输入身份证号"
                        
                    }
                    
     
                }else if indexPath.row == 2 {
                    creditScoreCell.titleLabel.text = "手机号码"
             
                    if  FCModelData.instance.phone.characters.count > 0 {
                        
                        creditScoreCell.titleText.text = FCModelData.instance.phone
                        
                    }else {
                        creditScoreCell.titleText.placeholder = "请输入手机号码"
                        
                    }
                    
                }else if indexPath.row == 3 {
                    creditScoreCell.titleLabel.text = "月收入"
                    creditScoreCell.titleText.isHidden = true
                    if FCModelData.instance.ysr.characters.count > 0 {
                        
                        creditScoreCell.resultLabel.text = FCModelData.instance.ysr
                        creditScoreCell.resultLabel.isHidden = false
                    }
                    creditScoreCell.accessoryType = .disclosureIndicator
                }else if indexPath.row == 4 {
                    creditScoreCell.titleLabel.text = "公司名称"
                   
                    if  FCModelData.instance.gongsi.characters.count > 0 {
                        
                        creditScoreCell.titleText.text = FCModelData.instance.gongsi
                        
                    }else {
                         creditScoreCell.titleText.placeholder = "请输入公司名称"
                        
                    }
                    
                }else if indexPath.row == 5 {
                    creditScoreCell.titleLabel.text = "工作年限"
                    if  FCModelData.instance.gongsidianhua.characters.count > 0 {
                        
                        creditScoreCell.titleText.text = FCModelData.instance.gongsidianhua
                        
                    }else {
                        creditScoreCell.titleText.placeholder = "请输入工作年限"
                        
                    }
                    
                }else if indexPath.row == 6 {
                    creditScoreCell.titleLabel.text = "名下有无房贷"
                    creditScoreCell.accessoryType = .disclosureIndicator
                    creditScoreCell.titleText.isHidden = true
                    if FCModelData.instance.fangdai.characters.count > 0 {
                        
                        
                        creditScoreCell.resultLabel.text = FCModelData.instance.fangdai
                        creditScoreCell.resultLabel.isHidden = false
                        
                        
                    }
                    
                }else if indexPath.row == 7 {
                    creditScoreCell.titleLabel.text = "名下有无房贷"
                    creditScoreCell.accessoryType = .disclosureIndicator
                    creditScoreCell.titleText.isHidden = true
                    if FCModelData.instance.chedai.characters.count > 0 {
                        
                        
                        creditScoreCell.resultLabel.text = FCModelData.instance.chedai
                        creditScoreCell.resultLabel.isHidden = false
                        
                        
                    }
                    
                }else if indexPath.row == 8 {
                    creditScoreCell.titleLabel.text = "有无连续6个月缴纳社保"
                    creditScoreCell.accessoryType = .disclosureIndicator
                    creditScoreCell.titleText.isHidden = true
                    if FCModelData.instance.shebao.characters.count > 0 {
                        
                        
                        creditScoreCell.resultLabel.text = FCModelData.instance.shebao
                        creditScoreCell.resultLabel.isHidden = false
                        
                    }
                    
                }else if indexPath.row == 9 {
                    creditScoreCell.accessoryType = .disclosureIndicator
                    creditScoreCell.titleLabel.text = "有无连续6个月缴纳住房公积金"
                    creditScoreCell.titleText.isHidden = true
                    if FCModelData.instance.gongjijin.characters.count > 0 {
                        
                        creditScoreCell.resultLabel.text = FCModelData.instance.gongjijin
                        creditScoreCell.resultLabel.isHidden = false
                        
                        
                    }
                    
                }
                
            }
            

            
        case 1:
            cell = tableView.dequeueReusableCell(withIdentifier: FCCreditScoreCellTwo.name,for: indexPath)
           
            if let twoCell = cell as? FCCreditScoreCellTwo {
                if indexPath.row == 0 {
                   
                    twoCell.delegate = self
                    twoCell.titleLabel.text = "是否持有信用卡"
                   
                    if FCModelData.instance.xinyongka == true {
                    
                       twoCell.oneButton.setImage(UIImage(named:"37"), for: .selected)
                    }else {
                    
                        twoCell.oneButton.setImage(UIImage(named:"36"), for: .normal)
                        twoCell.twoButton.setImage(UIImage(named:"36"), for: .normal)
                        
                        twoCell.oneButton.setImage(UIImage(named:"37"), for: .selected)
                        twoCell.twoButton.setImage(UIImage(named:"37"), for: .selected)
                        
                    
                    }
                    
                    if FCModelData.instance.yuqi == true {
                        
                        twoCell.twoButton.setImage(UIImage(named:"37"), for: .selected)
                    }else {
                        
                        twoCell.oneButton.setImage(UIImage(named:"36"), for: .normal)
                        twoCell.twoButton.setImage(UIImage(named:"36"), for: .normal)
                        
                        twoCell.oneButton.setImage(UIImage(named:"37"), for: .selected)
                        twoCell.twoButton.setImage(UIImage(named:"37"), for: .selected)
                        
                        
                    }
                    
                    
                    
                }else {
                    
                    twoCell.delegate = self
                    twoCell.titleLabel.text = "是否持有过逾期记录"
                    
                    if FCModelData.instance.xinyongka == true {
                        
                        twoCell.oneButton.setImage(UIImage(named:"37"), for: .selected)
                    }else {
                        
                        twoCell.oneButton.setImage(UIImage(named:"36"), for: .normal)
                        twoCell.twoButton.setImage(UIImage(named:"36"), for: .normal)
                        
                        twoCell.oneButton.setImage(UIImage(named:"37"), for: .selected)
                        twoCell.twoButton.setImage(UIImage(named:"37"), for: .selected)
                        
                        
                    }
                    
                    if FCModelData.instance.yuqi == true {
                        
                        twoCell.twoButton.setImage(UIImage(named:"37"), for: .selected)
                    }else {
                        
                        twoCell.oneButton.setImage(UIImage(named:"36"), for: .normal)
                        twoCell.twoButton.setImage(UIImage(named:"36"), for: .normal)
                        
                        twoCell.oneButton.setImage(UIImage(named:"37"), for: .selected)
                        twoCell.twoButton.setImage(UIImage(named:"37"), for: .selected)
                        
                        
                    }
                    

                    
                
                }
            }
            
            
            
        default:
            break
        }
        
        
        return cell
    
    }
    
    
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      
        if indexPath.section == 0 {
        if indexPath.row == 3 {
            let customView = CustomChooseTypeView.init(frame: CGRect(x: 0,y: 0,width: self.view.bounds.size.width,height: self.view.bounds.size.height))
            
            customView.creatWithDataAndTitle(data: array1, title: "请选择")
            customView.returnType = { (names) -> () in
                
                FCModelData.instance.ysr = names
                tableView.reloadData()
            }
            self.view.addSubview(customView)
            
            

        
        
        }else if indexPath.row == 6 {
        
            let customView = CustomChooseTypeView.init(frame: CGRect(x: 0,y: 0,width: self.view.bounds.size.width,height: self.view.bounds.size.height))
            
            customView.creatWithDataAndTitle(data: array2, title: "请选择")
            customView.returnType = { (names) -> () in
                 FCModelData.instance.fangdai = names
                tableView.reloadData()
            }
            self.view.addSubview(customView)
            
            

        }else if indexPath.row == 7 {
            let customView = CustomChooseTypeView.init(frame: CGRect(x: 0,y: 0,width: self.view.bounds.size.width,height: self.view.bounds.size.height))
            
            customView.creatWithDataAndTitle(data: array3, title: "请选择")
            customView.returnType = { (names) -> () in
                 FCModelData.instance.chedai = names
                    tableView.reloadData()
            }
            self.view.addSubview(customView)
            
            

            
        }else if indexPath.row == 8 {
            let customView = CustomChooseTypeView.init(frame: CGRect(x: 0,y: 0,width: self.view.bounds.size.width,height: self.view.bounds.size.height))
            
            customView.creatWithDataAndTitle(data: array5, title: "请选择")
            customView.returnType = { (names) -> () in
               FCModelData.instance.shebao = names
                tableView.reloadData()
            }
            self.view.addSubview(customView)
            
            

            
        }else if indexPath.row == 9 {
            let customView = CustomChooseTypeView.init(frame: CGRect(x: 0,y: 0,width: self.view.bounds.size.width,height: self.view.bounds.size.height))
            
            customView.creatWithDataAndTitle(data: array5, title: "请选择")
            customView.returnType = { (names) -> () in
                 FCModelData.instance.gongjijin = names
                tableView.reloadData()
            }
            self.view.addSubview(customView)
            
          }
        
        }
        
    }
    
    
    
    @IBAction func determineAction(_ sender: Any) {
   
        
        
        for textField in textFiledArr {
            
            let textResult = textField as! UITextField
            if textResult.tag == 0 {
                
                FCModelData.instance.name = textResult.text!
            }
        }

        for textField in textFiledArr {
            
            let textResult = textField as! UITextField
            if textResult.tag == 1 {
                
                FCModelData.instance.idNumber = textResult.text!
            }
        }

        for textField in textFiledArr {
            
            let textResult = textField as! UITextField
            if textResult.tag == 2 {
                
                FCModelData.instance.phone = textResult.text!
            }
        }
        for textField in textFiledArr {
            
            let textResult = textField as! UITextField
            if textResult.tag == 4 {
                
                FCModelData.instance.gongsi = textResult.text!
            }
        }
        for textField in textFiledArr {
            
            let textResult = textField as! UITextField
            if textResult.tag == 5 {
                
                FCModelData.instance.gongsidianhua = textResult.text!
            }
        }

        
        if FCModelData.instance.gongjijin.characters.count > 0 &&  FCModelData.instance.gongsi.characters.count > 0 && FCModelData.instance.yuqi == true {
            
            if let v = FCValueContoller.create() as? FCValueContoller {
            
                
                 FCModelData.instance.isTestCode = true

                
                
             self.navigationController?.pushViewController(v, animated: true)
            
            }
            
            
        }else {
        
            let alert = UIAlertController.init(title: "请输入完整信息！", message: nil, preferredStyle: .alert)
            let alertCancelAction: UIAlertAction = UIAlertAction.init(title: "确认", style: UIAlertActionStyle.cancel, handler: nil)
            
            alert.addAction(alertCancelAction)
            self.present(alert, animated: true, completion: nil)

        }
    }

    
    func changStart(sender: UIButton, one: UIButton, two: UIButton) {
        
        if one == sender {
            
        }else {
            
            one.setImage(UIImage(named:"36"),for: .normal)
            sender.setImage(UIImage(named:"37"),for: .normal)
            FCModelData.instance.xinyongka = true
       
        }
       
    
    
        if two == sender{
            
        }else {
            
            two.setImage(UIImage(named:"36"),for: .normal)
            sender.setImage(UIImage(named:"37"),for: .normal)
            FCModelData.instance.yuqi = true
        }
        
    }
    
        
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    
    
}
